/*

 Copyright (C) 2009 Team16
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. This program is distributed in the
hope that it will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License
for more details. You should have received a copy of the GNU General
Public License along with this program. If not, see <http://www.gnu.org/licens

  */

#include <QtGui>
 #include <QColor>
#include "highlighter.h"

//! [0]
Highlighter::Highlighter(QTextDocument *parent)
    : QSyntaxHighlighter(parent)
{


}



void Highlighter::init(QString lang){
       HighlightingRule rule=HighlightingRule();






highlightingRules.clear();




     QFile file( qApp->applicationDirPath()+   "/data/"+lang+".h");
     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
     {

     while (!file.atEnd()) {
         QString line = file.readLine();
         if (line.trimmed()!=""){
          QStringList rl=line.split("\t",QString::SkipEmptyParts);

           QStringList rl2=rl.at(0).split(" ",QString::SkipEmptyParts);



           keywordFormat.setForeground(QColor(rl.at(1)));
           keywordFormat.setFontItalic(rl.at(2)=="i");
           keywordFormat.setFontStrikeOut(rl.at(2)=="s");
           keywordFormat.setFontUnderline(rl.at(2)=="u");
           keywordFormat.setFontWeight(rl.at(3).toInt());

           foreach (const QString &pattern, rl2) {
               QString rul=pattern;
               rul.replace("%_%"," ",Qt::CaseInsensitive);
           rule.pattern=QRegExp(rul );
            rule.format = keywordFormat;
           rule.pattern.setMinimal(true);
            highlightingRules.append(rule);
       }

       }
     }
 }else{

     this->init("def");
 }
}

void Highlighter::highlightBlock(const QString &text)
{
    setCurrentBlockState(0);










    foreach (const HighlightingRule &rule, highlightingRules) {
        QRegExp expression(rule.pattern);
        int index = expression.indexIn(text);
        while (index >= 0) {
            int length = expression.matchedLength();
            setFormat(index, length, rule.format);
            index = expression.indexIn(text, index + length);
        }
    }




}
